package com.example.wechat

import android.content.ContentValues.TAG
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_home.*

import kotlinx.android.synthetic.main.fragment_down.*
import kotlinx.android.synthetic.main.layout_chat.*
import kotlinx.android.synthetic.main.me_layout.*

class HomeActivity<fragment : View?> : AppCompatActivity() {

    val chatInfos = arrayListOf<ChatInfo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        initData()
        Log.d(TAG, "onCreate: after initdata")
        val recycler = findViewById<RecyclerView>(R.id.recycler_chat)
        Log.d(TAG, "onCreate: after val recycler")
        recycler.adapter = ChatAdapter(chatInfos)
        Log.d(TAG, "onCreate: after recycler.adapter")
        recycler.layoutManager = LinearLayoutManager(this)

        btn_chat.setOnClickListener{
            me_layout.visibility=View.GONE
            recycler_chat.visibility=View.VISIBLE
            replaceFragment(UpFragment.ChatFragment())
        }
        btn_puq.setOnClickListener{
          //  framelayout_up.visibility=View.GONE
            recycler_chat.visibility=View.GONE
            replaceFragment(UpFragment.PuqFragment())
        }
        btn_friends.setOnClickListener{
          //  framelayout_up.visibility=View.GONE
            recycler_chat.visibility=View.GONE
            replaceFragment(UpFragment.FriendsFragment())
        }
        btn_me.setOnClickListener{
         //   framelayout_up.visibility=View.GONE
            recycler_chat.visibility=View.GONE
            replaceFragment(UpFragment.MeFragment())
        }

        replaceFragment(UpFragment())

    }

    private fun initData() {
        Log.d(TAG,"initdata")
        for(i in 0..10) {
            chatInfos.add(ChatInfo().apply {
                chatinfo_name = "MoonRain"
                chatinfo_time = "23:22"
                chatinfo_msg = "哈哈哈哈哈哈哈哈哈哈哈吼吼吼吼吼吼吼"
                chatinfo_headimg = R.mipmap.head1
            })
            chatInfos.add(ChatInfo().apply {
                chatinfo_name = "CandyTang"
                chatinfo_time = "23:12"
                chatinfo_msg = "太好笑了哈哈哈哈吼吼吼吼吼吼吼"
                chatinfo_headimg = R.mipmap.head2
            })
        }
        Log.d(TAG,"initdata success!")
        Log.d(TAG,"initdata size: ${chatInfos.size}")
    }

    private fun replaceFragment(fragment: Fragment) {

        val fragmentManager = supportFragmentManager
        val transaction = fragmentManager.beginTransaction()
        transaction.replace(R.id.framelayout_up,fragment)
        transaction.commit()

    }


}